/**
 * @Author: lll
 * @Email:li.lele@vpclub.cn
 * @Date: create in 2020/7/1
 */
package cn.vpclub.admin.page;

import cn.vpclub.admin.entity.ArticleEntity;
import cn.vpclub.admin.entity.BannerEntity;
import cn.vpclub.admin.entity.TabMenuEntity;
import cn.vpclub.admin.entity.UserEntity;
import cn.vpclub.admin.mapper.ArticleMapper;
import cn.vpclub.admin.mapper.BannerMapper;
import cn.vpclub.admin.mapper.TabMenuMapper;
import cn.vpclub.admin.mapper.UserMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.code.kaptcha.Producer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户页面
 *
 * @author 李乐乐
 */
@Controller
@RequestMapping(value = "home")
public class HomePage {
    Logger log = LoggerFactory.getLogger(HomePage.class);
    private Producer producer;

    @Autowired
    public void setCaptchaProducer(Producer producer) {
        this.producer = producer;
    }

    @Autowired
    BannerMapper bannerMapper;
    @Autowired
    TabMenuMapper tabMenuMapper;

    @Autowired
    ArticleMapper articleMapper;
    @Autowired
    UserMapper userMapper;

    /**
     * 首页
     *
     * @param model
     * @param httpServletRequest
     * @param httpServletResponse
     * @return
     */
    @RequestMapping(value = "")
    public String index(Model model, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        /**
         * banner位
         */
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("type", 1);
        List<BannerEntity> banner = bannerMapper.selectList(queryWrapper);
        /**
         * title栏目位
         */
        QueryWrapper queryWrapper1 = new QueryWrapper();
        List<TabMenuEntity> tabMenu = tabMenuMapper.selectList(queryWrapper1);
        model.addAttribute("banner", banner);
        model.addAttribute("tabMenu", tabMenu);
        return "home/index";
    }

    /**
     * 我的页面
     *
     * @return
     */
    @RequestMapping(value = "profile")
    public String profile() {
        return "home/profile";
    }

    /**
     * 注册页面
     *
     * @return
     */
    @RequestMapping(value = "register")
    public String register() {
        return "home/register";
    }

    /**
     * 登陆页面
     *
     * @return
     */
    @RequestMapping(value = "login")
    public String login() {
        return "home/login";
    }

    /**
     * 发表文章
     *
     * @return
     */
    @RequestMapping(value = "pushArticle")
    public String pushArticle() {
        return "home/pushArticle";
    }

    /**
     * 文章详情
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "articleDetail/{id}")
    public String articleDetail(@PathVariable String id, Model model) {
        log.info("id {}", id);
        ArticleEntity articleEntity = articleMapper.selectById(id);
        UserEntity userEntity = userMapper.selectById(articleEntity.getCreatedBy());
        model.addAttribute("article", articleEntity);
        model.addAttribute("user", userEntity);
        return "home/articleDetail";
    }

    /**
     * 二维码页面
     *
     * @param model
     * @param url
     * @param Authorization
     * @return
     */
    @RequestMapping(value = "/QRCode")
    public String QRCode(Model model, @RequestParam String url, @RequestParam String Authorization) {
        model.addAttribute("Authorization", Authorization);
        model.addAttribute("url", url);
        log.info("Authorization  {} ", Authorization);
        return "home/shareQRCode";
    }

    /**
     * 我的收藏页面
     *
     * @param model
     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "myCollection")
    public String myCollection(Model model, HttpServletRequest httpServletRequest) {
        return "home/myCollection";
    }

    /**
     * 我的点赞页面
     *
     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "myStared")
    public String myStared(Model model, HttpServletRequest httpServletRequest) {
        return "home/myStared";
    }

    /**
     * 我的文章页面
     *
     * @param model
     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "myArticle")
    public String myArticle(Model model, HttpServletRequest httpServletRequest) {
        return "home/myArticle";
    }

    /**
     * 文章分类详情
     *
     * @param model
     * @param httpServletRequest
     * @param title
     * @return
     */
    @RequestMapping(value = "menuDetail")
    public String menuDetail(Model model, HttpServletRequest httpServletRequest, @RequestParam(value = "title") String title) {
        model.addAttribute("title", title);
        return "home/menuDetail";
    }

    /**
     * 测试websocket
     *
     * @return
     */
    @RequestMapping(value = "admin")
    public String admin() {
        return "home/admin";
    }

    @RequestMapping(value = "messageCharts")
    public String messageCharts() {
        return "home/messageCharts";
    }
}
